<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="maximum-scale=1.0, minimum-scale=1.0, user-scalable=0, initial-scale=1.0, width=device-width" />
<meta name="format-detection" content="telephone=no, email=no, date=no, address=no">
<title>确认订单</title>
<link rel="stylesheet" type="text/css" href="../../css/aui-flex.css" />
<link rel="stylesheet" type="text/css" href="../../css/aui.css" />
<link rel="stylesheet" type="text/css" href="../../css/common.css" />
<style>
body{
	background: #f5f5f5;
}
.addrSec {
	padding: 0.75rem 1rem;
	margin-bottom: 0.4rem;
}
.address {
	padding-left: 1rem;
	padding-right: 1rem;
	min-height: 3rem;
}
.con{
	position: relative;
	color: #010101;
	font-size: 0.7rem;
}
.con:before {
	content: "";
	position: absolute;
	left: -1rem;
	top: 0.1rem;
	width: 0.68rem;
	height: 0.75rem;
	background: url(../../image/common/32.png) no-repeat;
	background-size: auto 0.75rem;
}
.addr {
	position: relative;
	margin-top: 0.5rem;
	color: #373336;
	font-size: 0.65rem;
	line-height: 20px;
}
.addr:before {
	content: "";
	position: absolute;
	left: -1rem;
	top: 0.1rem;
	width: 0.65rem;
	height: 0.75rem;
	background: url(../../image/common/33.png) no-repeat;
	background-size: auto 0.75rem;
}
.address .def {
	position: relative;
	top: -0.1rem;
	margin-left: 0.5rem;
	display: inline-block;
	font-size: 0.5rem;
	height: 0.8rem;
	line-height: 0.8rem;
	border-radius: 0.2rem;
	text-align: center;
	width: 1.45rem;
	vertical-align: middle;
}
.shop .item {
	padding: 0.9rem 1rem 0;
}
.shop .media {
	width: 3.6rem;
}
.textBox {
	min-height: 4.5rem;
	padding-left: 0.5rem;
}
.comBox{
	padding: 1.2rem 1rem 0;
}
.comBox .comMode{
	min-height: 2.1rem;
	line-height: 2.1rem;
	border-top: 1px solid #eee;
}
.comU{
	padding-top: 0.7rem;
	padding-bottom: 1rem;
}

.pay-list{
  padding: 0 .9rem;
  background-color: white;
  margin: .5rem 0;
}
.pay-list li{
  height: 2.5rem;
  /*padding: 0 0.75rem;*/
  font-size:0.7rem;
  color:#444444;
}
.pay-list li .imgBox{
  border-radius: 50%;
  margin-right: 0.3rem;
}
.pay-list li img{
  height: 1rem;
}
</style>
</head>
<body>
<div class="aui-content">
	<div class="addrSec bg-white" tapmode onclick="selectAddr();">
		<div class="blankAddr right-arrow aui-py-5">
			请填写收货人联系方式
		</div>
	</div>
	<script type="text/x-dot-template" id="addr_tpl">
	<div class="address aui-flex-row aui-flex-middle right-arrow">
		<div class="aui-flex-col aui-flex-between">
			<div class="con">{{= it.receive_name || ''}}</div>
			<div class="phone">{{= it.receive_mobile || ''}}</div>
		</div>
		<div class="addr">{{= it.receive_province || ''}}{{= it.receive_city || ''}}{{= it.receive_district || ''}}{{= it.receive_address || ''}}</div>
	</div>
	</script>
	<div id="data_html"></div>
	<!-- <div class="shop bg-white">
		<div class="item">
			<div class=" aui-flex-col aui-flex-middle">
				<div class="media"><img src="../../image/demo/03.png" alt=""></div>
				<div class="flex-auto aui-padded-l-10 aui-padded-r-15">
					<div class="aui-font-size-12 color-000 aui-ellipsis-2">心相印卷纸 心柔3层140g*27粒纸巾整箱 新老包装随机...</div>
					<div class="color-666 aui-font-size-12 aui-ellipsis">灰色</div>
					<div class="aui-flex-col aui-flex-between aui-flex-middle">
						<div class="color-red">¥dd</div>
						<div class="color-666">×1</div>
					</div>
				</div>
			</div>
		</div>
	</div>
	<div class="comBox bg-white">
		<div class="comMode aui-flex-col aui-flex-between aui-flex-middle aui-border-b">
			<div>配送方式</div>
			<div>快递免邮</div>
		</div>
		<div class="comU aui-flex-col aui-flex-right aui-flex-middle color-000 aui-font-size-12">
			<div>共2件商品</div>
			<div class="aui-padded-l-5">小计：<span class="text-bold">¥dd</span></div>
		</div>
	</div> -->
</div>
<script type="text/x-dot-template" id="data_tpl">
<div class="shop bg-white">
	{{ for(var i in it.data) { }}
	<div class="item">
		<div class="aui-flex-col aui-flex-middle">
			<div class="media"><img src="{{= it.data[i].goods_cover || '' }}" alt=""></div>
			<div class="flex-auto aui-padded-l-10 aui-padded-r-15">
				<div class="aui-font-size-12 color-000 aui-ellipsis-2">{{= it.data[i].goods_name || '' }}</div>
				<div class="color-666 aui-font-size-12 aui-ellipsis">{{= it.data[i].goods_spec_names || '' }}</div>
				<div class="aui-flex-col aui-flex-between aui-flex-middle">
					<div class="color-red">¥{{= it.data[i].goods_unit_price || '' }}</div>
					<div class="color-666">×{{= it.data[i].goods_count || '' }}</div>
				</div>
			</div>
		</div>
	</div>
	{{ } }}
</div>
<div class="comBox bg-white">
	<div class="comMode aui-flex-col aui-flex-between aui-flex-middle aui-border-b">
		<div>配送方式</div>
		<div>快递免邮</div>
	</div>
	<div class="comU aui-flex-col aui-flex-right aui-flex-middle color-000 aui-font-size-12">
		<div>共{{= it.count}}件商品</div>
		<div class="aui-padded-l-5">小计：<span class="text-bold">¥{{= it.totalPrice}}</span></div>
	</div>
</div>
<ul class="pay-list lastNoBorder">
	<li class="aui-flex-col aui-flex-middle aui-flex-between aui-border-b" tapmode onclick="checkPay(this)">
		<div class="imgBox"><img src="../../image/common/pay1.png" alt=""></div>
		<p class="flex-auto">微信支付</p>
		<div class="checkbox checked" data-type="1"></div>
	</li>
	<li class="aui-flex-col aui-flex-middle aui-border-b" tapmode onclick="checkPay(this)">
		<div class="imgBox"><img src="../../image/common/pay2.png" alt=""></div>
		<p class="flex-auto">支付宝支付</p>
		<div class="checkbox" data-type="2"></div>
	</li>
</ul>
</script>

</body>
<script type="text/javascript" src="../../script/import.js"></script>
<script type="text/javascript" src="../../script/common.js"></script>
<script type="text/javascript">
var isSave = false;
var CONFIRM_DATA;
var GOODS_INFO;
var TRADE_NO = '';
var PAY_TYPE = 0;
apiready = function() {
	GOODS_INFO = api.pageParam.goodsInfo;
	CONFIRM_DATA = {
		userid: getUserId(),
		goodsDatas: GOODS_INFO.data
	}
	showPage();

	getDefaultAddr();

	getPayType();

	// 接收选择地址广播
	api.addEventListener({
	    name: 'selectAddressEvent'
	}, function(ret, err) {
		var addrInfo = ret.value.info;
		// 订单postdata 更新
		CONFIRM_DATA.receive_name = addrInfo.name;
		CONFIRM_DATA.receive_mobile = addrInfo.mobile;
		CONFIRM_DATA.receive_province = addrInfo.prov;
		CONFIRM_DATA.receive_city = addrInfo.city;
		CONFIRM_DATA.receive_district = addrInfo.district;
		CONFIRM_DATA.receive_address = addrInfo.address;
		var tpl = $$('#addr_tpl').html();
		var tempFn = doT.template(tpl);
		$$('.addrSec').html(tempFn( CONFIRM_DATA ));
		api.parseTapmode();
	});
};
function showPage(){
	var tpl = $$('#data_tpl').html();
	var tempFn = doT.template(tpl);
	$$('#data_html').html(tempFn( GOODS_INFO ));
	api.parseTapmode();
}

function getDefaultAddr(){
	api.ajax({
		url: BASE_URL + 'App/User/addressList',
		method: 'post',
		data: {
			values: {
				userid: getUserId(),
				default: 1
			}
		}
	}, function(ret, err) {
		if (ret && ret.code == 1) {
			CONFIRM_DATA.receive_name = ret.data[0].address_username;
			CONFIRM_DATA.receive_mobile = ret.data[0].address_phone;
			CONFIRM_DATA.receive_province = ret.data[0].address_prov;
			CONFIRM_DATA.receive_city = ret.data[0].address_city;
			CONFIRM_DATA.receive_district = ret.data[0].address_district;
			CONFIRM_DATA.receive_address = ret.data[0].address_detail;
			var tpl = $$('#addr_tpl').html();
			var tempFn = doT.template(tpl);
			$$('.addrSec').html(tempFn( CONFIRM_DATA ));
			api.parseTapmode();
		}
	});
}

//选择地址
function selectAddr(){
	api.openWin({
		name: 'address_win',
		url: '../mine/address_win.html',
		slidBackEnabled: false,
		pageParam: {
			wname: api.winName,
			fname: api.frameName
		}
	});
}

function checkPay(el){
	var _this = $$(el).find('.checkbox');
	var payType = _this.data('type');
	// if(payType=='2'){
	// 	toast('该功能正在开发中，请选择微信支付');
	// 	return;
	// }
	$$('.pay-list .checkbox').removeClass('checked');
	_this.addClass('checked');
}
//获取订单类型 自营还是礼包 （有一件是大礼包  设置为礼包）
function getPayType(){
	PAY_TYPE = 0;
	for( var i=0;i<CONFIRM_DATA.goodsDatas.length;i++ ){
		if(CONFIRM_DATA.goodsDatas[i].goods_type == '1'){
			PAY_TYPE = 1;
		}
	}
}

function save(){
	if(isSave){
		return;
	}
	CONFIRM_DATA.payment = $$('.pay-list .checkbox.checked').data('type');
	if(!CONFIRM_DATA.receive_name || !CONFIRM_DATA.receive_mobile || !CONFIRM_DATA.receive_address ){
		toast('请完善收货人信息');
		return;
	}
	isSave = true;
	showProgress(true);

	api.ajax({
	    url: BASE_URL + 'App/Mall/orderAdd',
	    method: 'post',
	    data: {
	        values: CONFIRM_DATA
	    }
	},function(ret, err){
		isSave = false;
		hideProgress();
	    if (ret && ret.code == 1) {
			TRADE_NO = ret.data.tradeNo;
			api.sendEvent({
				name: 'addOrderSuccessEvent'
			});
			if(CONFIRM_DATA.payment == 1){
				pay_wx();
			}
			if(CONFIRM_DATA.payment == 2){
				var type = CONFIRM_DATA.goodsDatas.length > 1 ? 2 : 1;
				console.log(type)
				console.log(JSON.stringify(TRADE_NO))
				api.ajax({
					url: BASE_URL + 'App/Mall/aliPay',
					method: 'post',
					data: {
						values: {
							tradeNo: TRADE_NO,
							type: type
						}
					}
				},function(ret, err){
					// log(ret)
					// log(err)
					if (ret) {
						if(ret.code == 1){
								pay_ali(ret);
						}else{
							toast(ret.msg);
						}

					}
				});
			}
		}else{
			toast('系统异常');
		}
	});
}
// 微信支付
function pay_wx(){
	var notifyUrl = CONFIRM_DATA.goodsDatas.length > 1 ? wxPayUrl_2 : wxPayUrl_1;
	var wxPay = api.require('wxPay');
	wxPay.config({
		apiKey: '',
		mchId: '1516763411',
		partnerKey: '5ul24roehvlsoizd6ye94sh2ccl8s3v8',
		notifyUrl: notifyUrl
	}, function(result, err) {
		if (result.status) {
			wxPay.pay({
				description: '商品购买',
				totalFee: GOODS_INFO.totalPrice*100,
				// totalFee: 1,
				tradeNo: ''+ TRADE_NO +'',
				detail: '商品购买',
				feeType: 'CNY'
			}, function(ret, err) {
				if (ret.status) {
					orderSuccess();
					api.sendEvent({
							name: 'homeEvent'
					});
				} else {
					api.alert({
						title: '支付消息',
						msg: err.msg||'支付失败',
					}, function(ret, err) {
					});
				}
			});
		} else {
			toast('支付失败');
		}
	});
}
// 支付宝支付
function pay_ali(ret){
	log(ret.response);
	var aliPayPlus = api.require('aliPayPlus');
	aliPayPlus.payOrder({
		orderInfo: ret.response
	}, function(alipayRet, err) {
		if(alipayRet){
			switch (alipayRet.code) {
				case '9000':
					orderSuccess();
					break;
				case '4000':
					toast('支付失败');
					break;
				case '6001':
					toast('取消支付');
					break;
				default:
					toast('支付失败');
				break;
			}

		}else{
			toast('支付失败');
		}

	});
}
function orderSuccess(){
	api.sendEvent({
		name: 'orderPaySuccessEvent'
	});
	api.openWin({
		name: 'shop_success',
		url: 'shop_success.html',
		animation: {
			type: 'fade',
			duration: 200
		},
		bounces: false,
		bgColor: '#FFF',
		slidBackEnabled: false,
		pageParam: {
			payNum: GOODS_INFO.totalPrice,
			payType: PAY_TYPE
		}
	});
}

</script>
</html>
